namespace hw8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnVal_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            this.tbxResult.AppendText(button.Text.ToString());
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            if (this.tbxResult.Text.ToString() != "")
            {
                double result = 0.0;

                string expression = this.tbxResult.Text.ToString();
                this.tbxResult.AppendText(btnEqual.Text.ToString());
                this.tbxResult.AppendText(Environment.NewLine);
                Stack<char> operStack = new Stack<char>();
                Stack<string> numStack = new Stack<string>();
                char[] chars = expression.ToCharArray();
                foreach(char ch in chars)
                {
                    if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { operStack.Push(ch); }
                }
                string[] nums = expression.Split('+', '-', '*', '/');
                foreach(string num in nums)
                {
                    numStack.Push(num);
                }

                while (operStack.Count != 0)
                {
                    if (operStack.Peek() == '*' || operStack.Peek() == '/')
                    {
                        double num1 = Convert.ToDouble(numStack.Pop());
                        double num2 = Convert.ToDouble(numStack.Pop());
                        if (operStack.Pop() == '*')
                        {
                            numStack.Push((num1 * num2).ToString());
                        }
                        else
                        {
                            numStack.Push(((double)(num2 / num1)).ToString());
                        }
                    }
                    if (operStack.Peek() == '+' || operStack.Peek() == '-')
                    {
                        char oper = operStack.Pop();
                        double num = Convert.ToDouble(numStack.Pop());
                        if (operStack.Count != 0)
                        {
                            if (operStack.Peek() == '*' || operStack.Peek() == '/')
                            {
                                double num1 = Convert.ToDouble(numStack.Pop());
                                double num2 = Convert.ToDouble(numStack.Pop());
                                if (operStack.Pop() == '*')
                                {
                                    numStack.Push((num1 * num2).ToString());
                                }
                                else
                                {
                                    numStack.Push(((double)(num2 / num1)).ToString());
                                }
                                operStack.Push(oper);
                                numStack.Push(num.ToString());
                            }
                            else
                            {
                                double anum = Convert.ToDouble(numStack.Pop());
                                if (oper == '+') { result = (double)(num + anum); }
                                else { result = (double)(anum - num); }
                            }
                        }
                    }
                }

                this.tbxResult.AppendText(result.ToString());
            }
        }

        private void btnCls_Click(object sender, EventArgs e)
        {
            this.tbxResult.Clear();
        }
    }
}